﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;

namespace CSharp.Infrastructure
{
    public class JiguangAuthHelper
    {
        private static JavaScriptSerializer js = new JavaScriptSerializer() { MaxJsonLength = int.MaxValue };
        private string appid = string.Empty;
        private string secret = string.Empty;
        private string private_key_xml = string.Empty;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="appid"></param>
        /// <param name="secret"></param>
        /// <param name="private_key_xml">
        /// rsa private_key_xml 
        /// 通过 生成
        /// </param>
        public JiguangAuthHelper(string appid, string secret, string private_key_xml)
        {
            this.appid = appid;
            this.secret = secret;
            this.private_key_xml = private_key_xml;
        }

        public string GetLoginPhone(string loginToken)
        {
            string url = "https://api.verification.jpush.cn/v1/web/loginTokenVerify";
            string postData = @"{""loginToken"":""" + loginToken + @""", ""exID"":""""}";
            Dictionary<string, string> headers = new Dictionary<string, string>();
            headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(appid + ":" + secret)));

            // {"id":468707379853369344,"code":8000,"content":"get phone success","exID":"","phone":"XLGJ2OxtlcSiLSup4pW43qyTHpf20tDpEWiN9Ced+K/JIm3+ErrRpHpk/qULZhP2yJqGq558s7WDFnmWG2jXM1z2VlQ8C/dVD7UQ9k5vFwn77MxuMxw/9HFIMhIZ8kW+fOuaPnGlpG88epdULqUBDiC8SSOJUHddJ3Tvol5FpCY="}
            try
            {
                string result = HttpClientHelper.PostResponse_JSON(url, postData, headers);
                Console.WriteLine(result);

                var jiguang_info = js.Deserialize<jiguang_phone_result_info>(result);
                if (jiguang_info != null && jiguang_info.code == 8000)
                {
                    string phone = RSANewHelper.Decrypt(jiguang_info.phone, private_key_xml);

                    return phone;
                }
                else
                {
                    Console.WriteLine("jiguang_info " + result);

                    return string.Empty;
                }
            }
            catch (WebException webex)
            {
                try
                {
                    var result = new StreamReader(webex.Response.GetResponseStream()).ReadToEnd();
                    Console.WriteLine("jiguang_info ex " + result);

                    return string.Empty;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());

                    return string.Empty;
                }
            }
        }
    }

    /// <summary>
    /// 结果loginToken校验结果
    /// </summary>
    public class jiguang_phone_result_info
    {
        public int code { get; set; }

        public string phone { get; set; }

        public string content { get; set; }
    }
}
